package com.ng.application.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.google.android.material.tabs.TabLayout;
import com.ng.application.R;
import com.ng.application.adapter.MainTabAdapter;
import com.ng.application.fragment.HomeFragment;
import com.ng.application.fragment.MineFragment;
import com.ng.application.fragment.ServiceFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: Ng
 * @Date: 2024/4/9
 * @Versopn: 1.0.0
 * @Description:
 */
/**
 * MainActivity 是继承自 AppCompatActivity 的
 * AppCompatActivity 是 AndroidX 中提供的一种向下兼容的 Activity，可以使 Activity 在不同系统版本中的功能保持一致性
 * 而 Activity 类是 Android 系统提供的一个基类
 * 我们项目中所有自定义的 Activity 都必须继承它或者它的子类才能拥有 Activity 的特性（AppCompatActivity 是 Activity 的子类）
 */
public class MainActivity extends AppCompatActivity {

    private ViewPager viewPager;
    private TabLayout tabLayout;
    private List<Fragment> fragments;
    private final String[] titles = {"首页", "服务", "我的"};
    private final int[] selectIcon = {R.mipmap.tab_home_on, R.mipmap.tab_service_on, R.mipmap.tab_mine_on};
    private final int[] normalIcon = {R.mipmap.tab_home_off, R.mipmap.tab_service_off, R.mipmap.tab_mine_off};

    /**
     * 这个方法是一个 Activity 被创建时必定要执行的方法
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        findView();
    }

    private void findView() {
        viewPager = (ViewPager) findViewById(R.id.activity_main_view_pager);
        tabLayout = (TabLayout) findViewById(R.id.activity_main_tab_layout);

        initData();
    }

    public void initData() {
        fragments = new ArrayList<>();
        fragments.add(new HomeFragment());
        fragments.add(new ServiceFragment());
        fragments.add(new MineFragment());

        MainTabAdapter mainTabAdapter = new MainTabAdapter(getSupportFragmentManager(), this, tabLayout, titles, fragments, selectIcon, normalIcon);
        viewPager.setAdapter(mainTabAdapter);
        tabLayout.setupWithViewPager(viewPager);

        for (int index = 0; index < tabLayout.getTabCount(); index++) {
            TabLayout.Tab tab = tabLayout.getTabAt(index);
            tab.setCustomView(mainTabAdapter.getView(index));
        }

        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                View view = tab.getCustomView();
                ImageView imageView = view.findViewById(R.id.tab_item_icon);
                TextView textView = view.findViewById(R.id.tab_item_text);
                String title = textView.getText().toString();
                switch (title) {
                    case "首页":
                        imageView.setImageResource(selectIcon[0]);
                        break;
                    case "服务":
                        imageView.setImageResource(selectIcon[1]);
                        break;
                    case "我的":
                        imageView.setImageResource(selectIcon[2]);
                        break;
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                View view = tab.getCustomView();
                ImageView imageView = view.findViewById(R.id.tab_item_icon);
                TextView textView = view.findViewById(R.id.tab_item_text);
                String title = textView.getText().toString();
                switch (title) {
                    case "首页":
                        imageView.setImageResource(normalIcon[0]);
                        break;
                    case "服务":
                        imageView.setImageResource(normalIcon[1]);
                        break;
                    case "我的":
                        imageView.setImageResource(normalIcon[2]);
                        break;
                }
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }
}
